#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: 
@time: 2024/5/9 11:00
@Function: 存储构件管理页面对象
"""

import allure
from playwright.sync_api import Page, expect
from repository import componentUploadObject, componentOperateObject, componentListObject, componentBatchDeleteObject, \
    componentMoreObject, componentObject, componentSelectObject, componentPagingPageObject


class ComponentPage:
    def __init__(self, page: Page):
        self.page = page
        self.component_manage_loc = page.locator(componentObject.component_manage)
        self.component_upload_loc = page.locator(componentUploadObject.component_upload)
        self.component_convert_confirm_loc = page.locator(componentOperateObject.component_convert_confirm)
        self.component_convert_cancel_loc = page.locator(componentOperateObject.component_convert_cancel)
        self.component_convert_loc = page.locator(componentOperateObject.component_convert)
        self.component_status_loc = page.locator(componentListObject.component_status)
        self.component_upload_box_loc = page.locator(componentUploadObject.component_upload_box)
        self.component_batch_delete_loc = page.locator(componentBatchDeleteObject.component_batch_delete)
        self.component_allSelect_checkbox_loc = page.locator(componentListObject.component_allSelect_checkbox)
        self.component_preview_loc = page.locator(componentOperateObject.component_preview)
        self.component_preview_box_loc = page.locator(componentOperateObject.component_preview_box)
        self.component_preview_cancel_loc = page.locator(componentOperateObject.component_preview_cancel)
        self.component_file_name_loc = page.locator(componentListObject.component_file_name)
        # 构件搜索
        self.component_select_input_loc = page.get_by_placeholder(componentSelectObject.component_select_input)
        self.component_select_button_loc = page.get_by_role('button', name=componentSelectObject.component_select_button)
        self.component_select_reset_loc = page.locator(componentSelectObject.component_select_reset)
        self.component_select_setting_loc = page.locator(componentSelectObject.component_select_setting)
        self.component_select_setting_box_loc = page.locator(componentSelectObject.component_select_setting_box)
        self.component_select_setting_list_loc = page.locator(componentSelectObject.component_select_setting_list)
        self.component_select_setting_selectAll_input_loc = page.locator(componentSelectObject.component_select_setting_selectAll_input)
        self.component_select_setting_confirm_loc = page.locator(componentSelectObject.component_select_setting_confirm)
        self.component_select_setting_input_loc = page.locator(componentSelectObject.component_select_setting_name)
        self.component_status_dropdown_box_loc = page.locator(componentSelectObject.component_status_dropdown_box)
        self.component_status_list_content_loc = page.locator(componentSelectObject.component_status_list_content)
        self.component_more_loc = page.locator(componentOperateObject.component_more)
        # 构件操作栏更多操作
        self.component_more_box_loc = page.locator(componentMoreObject.component_more_box)
        self.component_more_list_loc = page.locator(componentMoreObject.component_more_list)
        self.component_detail_loc = page.locator(componentMoreObject.component_detail)
        self.component_detail_box_loc = page.locator(componentMoreObject.component_detail_box)
        self.component_detail_box_header_text_loc = page.locator(componentMoreObject.component_detail_box_header_text)
        self.component_detail_box_body_text_loc = page.locator(componentMoreObject.component_detail_box_body_text)
        self.component_detail_box_close_button_loc = page.locator(componentMoreObject.component_detail_box_close_button)
        self.component_share_loc = page.locator(componentMoreObject.component_share)
        self.component_share_box_loc = page.locator(componentMoreObject.component_share_box)
        self.component_share_box_header_text_loc = page.locator(componentMoreObject.component_share_box_header_text)
        self.component_share_box_body_text_loc = page.locator(componentMoreObject.component_share_box_body_text)
        self.component_share_periodOfValidity_oneDay_loc = page.locator(componentMoreObject.component_share_periodOfValidity_oneDay)
        self.component_share_periodOfValidity_sevenDay_loc = page.locator(componentMoreObject.component_share_periodOfValidity_sevenDay)
        self.component_share_periodOfValidity_foreverDay_loc = page.locator(componentMoreObject.component_share_periodOfValidity_foreverDay)
        self.component_share_set_havapassword_loc = page.locator(componentMoreObject.component_share_set_havapassword)
        self.component_share_set_password_loc = page.get_by_placeholder(componentMoreObject.component_share_set_password)
        self.component_share_box_confirm_button_loc = page.get_by_role('button', name=componentMoreObject.component_share_box_confirm_button)
        self.component_share_box_close_button_loc = page.get_by_role('button', name=componentMoreObject.component_share_box_close_button)
        self.component_render_info_loc = page.locator(componentMoreObject.component_render_info)
        self.component_render_info_box_loc = page.locator(componentMoreObject.component_render_info_box)
        self.component_render_info_box_header_text_loc = page.locator(componentMoreObject.component_render_info_box_header_text)
        self.component_render_info_box_body_text_loc = page.locator(componentMoreObject.component_render_info_box_body_text)
        self.component_render_info_box_close_button_loc = page.locator(componentMoreObject.component_render_info_box_close_button)
        self.component_rename_loc = page.locator(componentMoreObject.component_rename)
        self.component_rename_box_loc = page.locator(componentMoreObject.component_rename_box)
        self.component_rename_box_input_loc = page.get_by_placeholder(componentMoreObject.component_rename_box_input)
        self.component_rename_box_confirm_loc = page.get_by_role('button', name=componentMoreObject.component_rename_box_confirm)
        self.component_rename_box_cancel_loc = page.get_by_role('button', name=componentMoreObject.component_rename_box_cancel)
        self.component_rename_box_close_loc = page.locator(componentMoreObject.component_rename_box_close)
        self.component_delete_loc = page.locator(componentMoreObject.component_delete)
        self.component_delete_box_loc = page.locator(componentMoreObject.component_delete_box)
        self.component_delete_box_text_loc = page.locator(componentMoreObject.component_delete_box_text)
        self.component_delete_success_text_loc = page.locator(componentMoreObject.component_delete_success_text)
        self.component_delete_confirm_button_loc = page.get_by_role('button', name=componentMoreObject.component_delete_confirm_button)
        # 分页操作
        self.component_page_size_loc = page.locator(componentPagingPageObject.component_page_size)
        self.component_page_size_text_loc = page.locator(componentPagingPageObject.component_page_size_text)
        self.component_page_size_box_loc = page.locator(componentPagingPageObject.component_page_size_box)
        self.component_page_size_list_loc = page.locator(componentPagingPageObject.component_page_size_list)
        self.component_page_previous_page_loc = page.locator(componentPagingPageObject.component_page_previous_page)
        self.component_page_next_page_loc = page.locator(componentPagingPageObject.component_page_next_page)
        self.component_page_3_loc = page.locator(componentPagingPageObject.component_page_3)
        self.component_page_jump_input_loc = page.locator(componentPagingPageObject.component_page_jump_input)


    @allure.step('进入构件管理模块')
    def navigate(self, envir):
        # 测试环境
        try:
            if envir == "test":
                self.page.goto(
                    'http://xbasetest.dxbim.com:18898/console/component-manage')
                self.component_manage_loc.wait_for(state="visible")
            # stage环境
            elif envir == "stage":
                self.page.goto(
                    'https://xbasestage.dxbim.com:18899/console/component-manage')
        except Exception as e:
            print('navigate方法存在问题，路径可能更新了')

    def component_query_setting_box(self):
        """
        对应构件管理查询设置中的公共方法:打开查询设置弹窗
        :return:
        """
        self.component_select_setting_loc.click()
        expect(self.component_select_setting_selectAll_input_loc).to_be_visible()
        # self.component_select_setting_selectAll_input_loc.click()
        # self.component_select_setting_selectAll_input_loc.click()

    def component_query_setting_box_selectAll(self):
        """
        对应构件管理查询设置中的公共方法:将所有查询项展示出来
        :return:
        """
        self.component_select_setting_loc.click()
        expect(self.component_select_setting_selectAll_input_loc).to_be_visible()
        self.component_select_setting_selectAll_input_loc.click()
        self.component_select_setting_confirm_loc.click()

    def component_query_name(self, component_name):
        """
        根据构件名称进行查询
        :param component_name:
        :return:
        """
        self.page.locator(
            "//div[@class='search-container']/div[@class='mms-row']//div[1]//div//input".format(
            )).fill(component_name)

    def component_query_ID(self, component_ID):
        """
        根据构件ID进行查询
        :param component_ID:
        :return:
        """
        self.page.locator(
            "//div[@class='search-container']//div[@class='ant-row')]//div[@label='构件ID')]//span".format(
            )).fill(component_ID)

    @allure.step('利用查询设置中的选项内容进行搜索')
    def component_query_setting_func(self, component_query_select_name):
        """
        针对构件管理查询设置重构成统一的方法
        功能：传入查询设置中的选项名称，来自动匹配对应的内容
        :return:
        """
        self.component_select_setting_loc.click()
        expect(self.component_select_setting_box_loc).to_be_visible()
        self.component_select_setting_selectAll_input_loc.click()
        self.component_select_setting_selectAll_input_loc.click()
        component_query_select_lists = self.component_select_setting_list_loc.count()
        for x in range(1, component_query_select_lists + 1):
            if component_query_select_name == self.page.locator(
                    "//div[@class='mms-popover-content']//div[@class='mms-popover-inner-content']//div[@class='mms-checkbox-group']/div[{}]//span[2]".format(
                        x)).inner_text():
                self.page.locator(
                    "//div[@class='mms-popover-content']//div[@class='mms-popover-inner-content']//div[@class='mms-checkbox-group']/div[{}]//span[1]/input ".format(
                        x)).check()
                break
        self.component_select_setting_confirm_loc.click()
        # 查看列表的数量
        component_list_numbers = self.page.locator('//tbody[@class="mms-table-tbody"]/tr').count()
        assert component_list_numbers >= 0


    @allure.step('在构件管理搜索框中输入搜索内容')
    def component_query_input(self, component_info):
        """
        对应构件管理中搜索的input输入框：包括构件名称、构件ID等
        :return:
        """
        self.component_select_input_loc.clear()
        self.component_select_input_loc.fill(component_info)

    @allure.step('依据构件状态来过滤构件列表数据')
    def component_status_select(self, component_status_info):
        """
        对应查询设置中选择构件状态，通过传递的构件状态来过滤构件列表数据
        :param component_status_info:
        :return:
        """
        self.component_status_dropdown_box_loc.click()
        component_status_list = self.component_status_list_content_loc.count()
        for x in range(1, component_status_list + 1):
            component_status = self.page.locator(
                componentSelectObject.component_status_list_content + "[{}]".format(x)).inner_text()
            if component_status_info == component_status:
                self.page.locator(componentSelectObject.component_status_list_content + "[{}]".format(x)).click()
                break

    @allure.step('点击查询按钮')
    def component_query_button(self):
        """
        对应构件管理中搜索的查询按钮
        :return:
        """
        self.component_select_button_loc.click()
        component_list_numbers = self.page.locator('//tbody[@class="mms-table-tbody"]/tr').count()
        assert component_list_numbers >= 0

    @allure.step('重置构件搜索内容')
    def component_query_reset(self):
        """
        对应构件管理中搜索的重置
        :return:
        """
        self.component_select_reset_loc.click()
        component_list_numbers = self.page.locator('//tbody[@class="mms-table-tbody"]/tr').count()
        expect(self.component_select_input_loc).to_have_value('')
        assert component_list_numbers >= 1

    @allure.step("构件上传")
    def component_upload(self, component_file):
        self.page.set_input_files(componentUploadObject.component_upload, component_file)
        self.component_upload_box_loc.wait_for(state='visible')
        self.component_upload_box_loc.wait_for(state='hidden')
        expect(self.component_status_loc).to_have_text(['上传成功'], timeout=2000)

    @allure.step("构件转换")
    def component_conver(self, is_convert):
        component_state = self.component_status_loc.inner_text()
        try:
            if component_state == '上传成功':
                if is_convert is True:
                    self.component_convert_loc.click()
                    self.component_convert_confirm_loc.wait_for(state='visible', timeout=2000)
                    expect(self.component_convert_confirm_loc).to_be_visible()
                    self.component_convert_confirm_loc.click()
                    self.component_convert_confirm_loc.wait_for(state='hidden')
                    while True:
                        component_conver_state = self.component_convert_loc.inner_text()
                        if component_conver_state == '重新转换':
                            break
                else:
                    self.component_convert_cancel_loc.click()
        except Exception as e:
            print(f"构件转换出现异常:{e}")

    @allure.step("构件预览")
    def component_preview(self):
        component_state = self.component_status_loc.inner_text()
        try:
            if component_state == '转换成功':
                self.component_preview_loc.click()
                # self.component_preview_box_loc.wait_for(state='visible', timeout=5000)
                expect(self.component_preview_box_loc).to_be_visible(timeout=5000)
                self.component_preview_cancel_loc.click()
                self.component_preview_box_loc.wait_for(state='hidden')
            else:
                expect(self.component_preview_loc).to_be_disabled()
        except Exception as e:
            print(f"构件预览出现异常:{e}")

    def component_more_box(self):
        """
        对应判断构件列表更多下拉框是否出现-提取的公共方法
        :return:
        """
        self.component_more_loc.hover()
        self.page.wait_for_selector(componentMoreObject.component_more_box, state="attached")
        expect(self.component_more_box_loc).to_be_visible()

    @allure.step("查看构件详情")
    def component_detail(self):
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == '转换成功':
                self.component_detail_loc.wait_for(state="visible")
                self.component_detail_loc.hover()
                self.component_detail_loc.click()
                expect(self.component_detail_box_loc).to_be_visible()
                expect(self.component_detail_box_header_text_loc).to_have_text("转换详情")
                expect(self.component_detail_box_body_text_loc).to_contain_text(
                    ["转换总时长", "视图类型", "BIM可视化转换参数", "开始转换", "转换成功"])
                self.component_detail_box_close_button_loc.click()
            elif component_state == '转换失败':
                self.component_detail_loc.wait_for(state="visible")
                self.component_detail_loc.hover()
                self.component_detail_loc.click()
                expect(self.component_detail_box_loc).to_be_visible()
                expect(self.component_detail_box_header_text_loc).to_have_text("转换详情")
                expect(self.component_detail_box_body_text_loc).to_contain_text(
                    ["转换总时长", "视图类型", "BIM可视化转换参数", "开始转换", "转换失败"])
                self.component_detail_box_close_button_loc.click()
            else:
                expect(self.component_detail_loc).to_be_disabled()
        except Exception as e:
            print(f"查看构件详情出现异常:{e}")

    @allure.step('设置资产分享中的有分享时效')
    def component_share_set_validity_period(self, share_validity_period=1):
        """
        对应构件管理更多中的分享设置有效期-默认1天和无密码
        :param share_validity_period:
        :return:
        """
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == "转换成功":
                self.component_share_loc.wait_for(state="visible")
                self.component_share_loc.hover()
                self.component_share_loc.click()
                expect(self.component_share_box_loc).to_be_visible()
                expect(self.component_share_box_header_text_loc).to_contain_text('分享')
                expect(self.component_share_box_body_text_loc).to_contain_text(["分享内容", "分享时效", "分享密码"])
                if share_validity_period == 1:
                    self.component_share_periodOfValidity_oneDay_loc.click()
                elif share_validity_period == 7:
                    self.component_share_periodOfValidity_sevenDay_loc.click()
                else:
                    self.component_share_periodOfValidity_foreverDay_loc.click()
            else:
                expect(self.component_share_loc).to_be_disabled()
        except Exception as e:
            print(f"构件分享出现异常:{e}")

    @allure.step('设置构件分享中的分享密码')
    def component_share_set_password(self, pwd_info, share_pwd='无'):
        """
        对应构件管理更多中的分享-设置分享密码，默认无密码
        :param pwd_info:
        :param share_pwd:
        :return:
        """
        if share_pwd == '有':
            self.component_share_set_havapassword_loc.click()
            self.component_share_set_password_loc.clear()
            self.component_share_set_password_loc.fill(pwd_info)
        self.component_share_box_confirm_button_loc.click()
        self.component_share_box_close_button_loc.click()

    @allure.step("查看构件渲染信息")
    def component_renderInfo(self):
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == '转换成功':
                self.component_render_info_loc.wait_for(state="visible")
                self.component_render_info_loc.hover()
                self.component_render_info_loc.click()
                expect(self.component_render_info_box_loc).to_be_visible()
                expect(self.component_render_info_box_header_text_loc).to_have_text("渲染信息")
                expect(self.component_render_info_box_body_text_loc).to_contain_text(
                    ["构件ID", "渲染路径"])
                self.component_render_info_box_close_button_loc.click()
            else:
                expect(self.component_render_info_loc).to_be_disabled()
        except Exception as e:
            print(f"查看构件渲染信息出现异常:{e}")

    @allure.step("关闭构件重命名")
    def component_rename_close(self):
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == '转换成功':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_close_loc.click()
            elif component_state == '转换失败':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_close_loc.click()
            else:
                expect(self.component_detail_loc).to_be_disabled()
        except Exception as e:
            print(f"构件重命名出现异常:{e}")

    @allure.step("取消构件重命名")
    def component_rename_cancel(self):
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == '转换成功':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_cancel_loc.click()
            elif component_state == '转换失败':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_cancel_loc.click()
            else:
                expect(self.component_detail_loc).to_be_disabled()
        except Exception as e:
            print(f"构件重命名出现异常:{e}")

    @allure.step("确定构件重命名")
    def component_rename_confirm(self, component_rename_info):
        component_state = self.component_status_loc.inner_text()
        self.component_more_box()
        try:
            if component_state == '转换成功':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_input_loc.clear()
                self.component_rename_box_input_loc.fill(component_rename_info)
                self.component_rename_box_confirm_loc.click()
            elif component_state == '转换失败':
                self.component_rename_loc.wait_for(state="visible")
                self.component_rename_loc.hover()
                self.component_rename_loc.click()
                expect(self.component_rename_box_loc).to_be_visible()
                self.component_rename_box_input_loc.clear()
                self.component_rename_box_input_loc.fill(component_rename_info)
                self.component_rename_box_confirm_loc.click()
            else:
                expect(self.component_detail_loc).to_be_disabled()
        except Exception as e:
            print(f"构件重命名出现异常:{e}")

    def component_more_list(self, component_more_list_name, component_status):
        """
        获取构件列表更多选项下拉内容-公共方法
        :param component_more_list_name: 传入在更多下拉内容中要选择的操作内容，比如：更多下拉框中的详情
        :return:
        """
        self.component_more_box()
        component_more_lists = self.component_more_list_loc.count()
        print("===>", component_more_lists)
        for x in range(1, component_more_lists + 1):
            if x == 5 and component_status == "转换成功":
                continue
            elif x <= 5 and component_status == "待转换":
                continue
            component_more_list_content = self.page.locator(
                componentMoreObject.component_more_list + "[{}]//span".format(x)).inner_text()
            if component_more_list_content.strip() == component_more_list_name:
                self.page.locator(
                    componentMoreObject.asset_more_list + "[{}]//span".format(x)).hover()
                self.page.locator(
                    componentMoreObject.asset_more_list + "[{}]//span".format(x)).click()
                break

    @allure.step('从构件列表更多中删除构件')
    def component_delete(self):
        """
        对应构件管理列表中操作栏更多中的删除

        :return:
        """
        self.component_more_box()
        self.component_delete_loc.wait_for(state="visible")
        self.component_delete_loc.hover()
        self.component_delete_loc.click()
        expect(self.component_delete_box_loc).to_be_visible()
        expect(self.component_delete_box_text_loc).to_have_text("此删除操作不可恢复，是否继续？")
        self.component_delete_confirm_button_loc.click()
        self.component_delete_box_loc.wait_for(state='hidden')
        delete_succ_info = self.component_delete_success_text_loc.inner_text()
        assert delete_succ_info == '删除成功'

    @allure.step("构件批量删除")
    def component_batch_delete(self):
        self.component_allSelect_checkbox_loc.hover()
        self.component_allSelect_checkbox_loc.check()
        self.component_batch_delete_loc.click()
        expect(self.component_delete_box_loc).to_be_visible()
        expect(self.component_delete_box_text_loc).to_have_text("此删除操作不可恢复，是否继续？")
        self.component_delete_confirm_button_loc.click()
        self.component_delete_box_loc.wait_for(state='hidden')
        delete_succ_info = self.component_delete_success_text_loc.inner_text()
        assert delete_succ_info == '删除成功'

    @allure.step("查看构件列表下一页")
    def component_next_page(self):
        component_name_first = self.component_file_name_loc.inner_text()
        self.component_page_next_page_loc.click()
        component_name_second = self.component_file_name_loc.inner_text()
        assert component_name_first != component_name_second

    @allure.step("查看构件列表上一页")
    def component_previous_page(self):
        component_name_first = self.component_file_name_loc.inner_text()
        self.component_page_previous_page_loc.click()
        component_name_second = self.component_file_name_loc.inner_text()
        assert component_name_first != component_name_second

    @allure.step("点击构件列表第3页")
    def component_click_page(self):
        component_name_first = self.component_file_name_loc.inner_text()
        self.component_page_3_loc.click()
        component_name_second = self.component_file_name_loc.inner_text()
        assert component_name_first != component_name_second

    @allure.step('选择不同的页数量来展示构件列表')
    def component_page_size_select(self, component_list_size):
        """
        对应构件列表分页操作，通过传递的每页的数量来展示构件列表
        :param component_list_size:
        :return:
        """
        self.component_page_size_loc.click()
        expect(self.component_page_size_box_loc).to_be_visible()
        component_page_size_list_number = self.component_page_size_list_loc.count()
        for x in range(1, component_page_size_list_number + 1):
            component_size = self.page.locator(
                componentPagingPageObject.component_page_size_list + "[{}]/div".format(x)).inner_text()
            if component_list_size == component_size:
                self.page.locator(componentPagingPageObject.component_page_size_list + "[{}]/div".format(x)).click()
                assert self.component_page_size_text_loc.inner_text() == component_list_size
                break

    @allure.step('跳转到**页来展示构件列表')
    def component_page_jump(self, component_page_info):
        """
        对应构件列表分页操作，通过传递的页数来跳转到对应的页面展示构件列表
        :param component_page_info:
        :return:
        """
        component_name_first = self.component_file_name_loc.inner_text()
        self.component_page_jump_input_loc.clear()
        self.component_page_jump_input_loc.fill(component_page_info)
        self.page.keyboard.press('Enter')
        component_name_second = self.component_file_name_loc.inner_text()
        assert component_name_first != component_name_second


